package com.example.demo.DBServer.thisDatabase.service.impl;

import com.example.demo.DBServer.thisDatabase.dao.RelatedExecuteInformationDao;
import com.example.demo.DBServer.thisDatabase.service.RelatedExecuteInformationService;
import com.example.demo.entity.RelatedExecuteInformationEntity;
import com.example.demo.utils.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class RelatedExecuteInformationServiceImpl implements RelatedExecuteInformationService {

    @Autowired
    private RelatedExecuteInformationDao relatedExecuteInformationDao;

    @Override
    public void saveRelatedExecuteInformation(RelatedExecuteInformationEntity information) {
        relatedExecuteInformationDao.saveRelatedExecuteInformation(information);
        if(CollectionUtil.isNotEmpty(information.getRelatedExecuteInformation())){
            information.getRelatedExecuteInformation().forEach(info -> {
                info.setExecuteInformationId(information.getExecuteInformationId());
                info.setParentRelatedId(information.getId());
            });
            batchSaveRelatedExecuteInformation(information.getRelatedExecuteInformation());
        }
    }

    @Override
    public void batchSaveRelatedExecuteInformation(List<RelatedExecuteInformationEntity> list) {
        if(list==null || list.size()==0){
            return;
        }
        relatedExecuteInformationDao.batchSaveRelatedExecuteInformation(list);
        List<RelatedExecuteInformationEntity> needAddList = new ArrayList<>();
        for(RelatedExecuteInformationEntity information : list){
            if(CollectionUtil.isNotEmpty(information.getRelatedExecuteInformation())){
                information.getRelatedExecuteInformation().forEach(info -> {
                    info.setExecuteInformationId(information.getExecuteInformationId());
                    info.setParentRelatedId(information.getId());
                });
                needAddList.addAll(information.getRelatedExecuteInformation());
            }
        }
        batchSaveRelatedExecuteInformation(needAddList);
    }

    @Override
    public RelatedExecuteInformationEntity findOneById(Long id) {
        return relatedExecuteInformationDao.findOneById(id);
    }

    @Override
    public List<RelatedExecuteInformationEntity> findAllByExecuteInformationId(Long executeInformationId) {
        return relatedExecuteInformationDao.findAllByExecuteInformationId(executeInformationId);
    }
}
